var menuIds;
$(function () {
    var roleId = $("#roleId").val();
    getMenuTreeData(roleId);
    $("#submit").click(function () {
        save(roleId);
    });
});
var getMenuTreeData = function (roleId) {
    $.get("/system/menu/tree/" + roleId, function (res) {
        loadTree(res.data);
    }, 'json');
};

var loadTree = function (tree) {
    $('#menuTree').jstree({
        'core': {
            'data': tree
        },
        "checkbox": {
            // "three_state": true,
        },
        "plugins": ["wholerow", "checkbox"]
    });
    $('#menuTree').jstree('open_all');
};

function getAllSelectNodes() {
    var ref = $('#menuTree').jstree(true); // 获得整个树
    menuIds = ref.get_selected(); // 获得所有选中节点的，返回值为数组
    $("#menuTree").find(".jstree-undetermined").each(function (i, element) {
        menuIds.push($(element).closest('.jstree-node').attr("id"));
    });
}

function save(roleId) {
    getAllSelectNodes();
    $.post("/system/role/saveMenus", {roleId: roleId, menuIds: menuIds}, function (res) {
        if (res.success) {
            parent.layer.msg("操作成功");
            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
            parent.layer.close(index);
        } else {
            parent.layer.msg(res.data.errMsg);
        }
    }, 'json');
}



